<?php

namespace App\Admin\Controllers;

use App\Models\Cate;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class CateController extends Controller
{
    use HasResourceActions;
    use CateTrait;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('分类列表')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑')
            ->body($this->form(true)->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新增')
            ->body($this->form(false));
    }

    /**
     * 删除方法
     */
    public function destroy($id)
    {

        $cate=Cate::findOrFail($id);
        if($cate->children->toArray()){
            admin_error('错误提示', '该分类下有子类，不能删除');
        }elseif($cate->articles->toArray()){
            admin_error('错误提示', '该分类下有文章，不能删除');
        }else{
            $cate->delete();
        }

    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Cate);


        $grid->id('ID')->sortable();
        $grid->name('名称');


        $grid->thumb('缩略图')->display(function($value){
            return $value?'是':'否';
        });

        $grid->level('层级');

        $grid->column('full_name','层级路径')->display(function(){
            return $this->full_name;
        });

        $grid->actions(function ($actions){
            // append一个操作
            $actions->disableView();
            $actions->append('<a href="'.url('/admin/cates/create?pid='.$actions->getKey()).'"><i class="fa fa-child"></i></a>');

        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Cate::findOrFail($id));

        $show->id('Id');
        $show->name('Name');
        $show->parent_id('Parent id');
        $show->is_directory('Is directory');
        $show->level('Level');
        $show->path('Path');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form($isEditing=false)
    {
        $form = new Form(new Cate);
        $form->display('id', 'ID');

        $form->text('name','名称')->rules(function($form){
            if($id=$form->model()->id){
                return 'required|unique:cates,name,'.$id.',id';
            }else{
                return 'required|unique:cates';
            }
        });


        if($isEditing){
            //编辑
            //支持用符号 . 来展示关联关系的字段
            $form->display('parent.name','父类目');
        }else{
            //新增

            //获取类的层次结构
            $pid=request()->pid?:0;
            $options=$this->getCateArrange();
            $form->select('parent_id','父级分类')->options($options)->default($pid);
        }

        $image=$form->image('thumb','缩略图');
        $image->move('cates');
        $image->uniqueName();

        return $form;
    }
}
